package com.yike.base.service.impl;

import com.yike.base.mapper.AreaMapper;
import com.yike.base.service.AreaService;
import com.yike.base.vo.AreaTreeVo;
import com.yike.base.vo.AreaVo;
import com.yike.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务实现类
 *
 * @author Sakura
 * @since 2024-08-12
 */
@Slf4j
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<AreaVo> getSubAreas(Integer parentId) {
        return areaMapper.getSubAreas(parentId);
    }

    @Override
    public List<AreaTreeVo> getAreas() throws Exception {
        // 先获取所有的区域信息
        List<AreaVo> areaVos = areaMapper.getAreas();
        int num = 0; // 添加一个遍历层数，防止数据异常导致递归死循环
        List<AreaTreeVo> areaTreeVos = getChildAreas(0, areaVos, num);
        return areaTreeVos;
    }

    private List<AreaTreeVo> getChildAreas(Integer parentId, List<AreaVo> areas, int num) throws Exception {
        num++;// 控制遍历次数，防止因数据问题导致无限循环内存溢出，目前最大支持10层
        if (num > 10) {
            log.error("区域数据parentId：" + parentId);
            throw new BusinessException(500, "区域数据异常，请联系管理人员");
        }

        List<AreaTreeVo> resultList = new ArrayList<>();

        for (AreaVo areaVo : areas) {
            if (parentId.equals(areaVo.getParentId())) {
                AreaTreeVo areaTreeVo = new AreaTreeVo();
                BeanUtils.copyProperties(areaVo, areaTreeVo);
                // 通过当前ID获取子权限
                List<AreaTreeVo> childAreas = getChildAreas(areaVo.getId(), areas, num);
                areaTreeVo.setChildren(childAreas);

                resultList.add(areaTreeVo);
            }
        }

        return resultList;
    }
}
